package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeCountVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserLikeManager {

    @Reference
    private UserLikeService userLikeService;
    @Reference
    private FriendService friendService;
    @Reference
    private UserInfoService userInfoService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    //探花-右滑(添加喜欢)
    public void love(Long likeUserId) {
        //1.获取登录用户id
        Long userId = UserHolder.get().getId();

        //2.调用userLikeService生成(插入)喜欢记录
        userLikeService.save(userId,likeUserId);

        //3.判断一下二人是否相互喜欢
        boolean isMutualLike = userLikeService.isMutualLike(userId,likeUserId);

        //4.如果是(即isMutualLike是true)
            if(isMutualLike){
                // 1).在friend表中维护(插入)好友关系记录
                friendService.save(userId,likeUserId);
                // 2).在环信中记录好友关系
                huanXinTemplate.addContacts(userId,likeUserId);
            }

    }

    //探花-左滑(删除喜欢)
    public void deleteUserLike(Long likeUserId) {
        //1.获取登录用户id
        Long userId = UserHolder.get().getId();

        //2.调用userLikeService删除user_like表中好友关系
        userLikeService.delete(userId,likeUserId);

        //3.调用friendService删除friend表中好友关系
        friendService.delete(userId,likeUserId);

        //4.删除环信里的好友关系
        huanXinTemplate.deleteContacts(userId,likeUserId);
    }

    //统计用户的喜欢 朋友 粉丝数量
    public UserLikeCountVo findUserCounts() {
        //1.获取用户登录id
        Long userId = UserHolder.get().getId();
        //2.调用userLikeService查询统计的数值
        return userLikeService.findUserCounts(userId);
    }

    //互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
    public PageBeanVo findUserList(Integer type, Integer pageNum, Integer pageSize) {
        //1.获取登录用户id
        Long userId = UserHolder.get().getId();

        //2.调用userLikeService查询结果(查到的用户id和缘分值封装进去)
        PageBeanVo  pageBeanVo=userLikeService.findUserList(userId,type,pageNum,pageSize);
        List<UserLikeVo> userLikeVoList = (List<UserLikeVo>) pageBeanVo.getItems();
        //3.调用userInfoService把个人信息填充起来
        if (CollectionUtil.isNotEmpty(userLikeVoList)){
            for (UserLikeVo userLikeVo : userLikeVoList) {
                UserInfo userInfo = userInfoService.findByUserId(userLikeVo.getId());
                BeanUtil.copyProperties(userInfo,userLikeVo);
            }
        }
        //4.返回Vo
        return pageBeanVo;
    }
}
